package core

import (
	"github.com/spf13/viper"
	"log"
)

var Conf *Config

type Config struct {
	Server    *Server    `yaml:"server"`
	DataBase  *DataBase  `yaml:"database"`
	Redis     *Redis     `yaml:"redis"`
	Cache     *Cache     `yaml:"cache"`
	Es        *Es        `yaml:"es"`
	ZapLog    *ZapLog    `yaml:"zapLog"`
	LogrusLog *LogrusLog `yaml:"logrusLog"`
	JWTAuth   *JWTAuth   `yaml:"JWTAuth"`
}

type Server struct {
	Port       string `yaml:"port"`
	Mode       string `yaml:"dev"`
	Version    string `yaml:"version"`
	SigningKey string `yaml:"signingKey"`
}

type DataBase struct {
	Driver              string `yaml:"driver"`
	Host                string `yaml:"host"`
	Port                string `yaml:"port"`
	DbName              string `yaml:"dbName"`
	Username            string `yaml:"username"`
	Password            string `yaml:"password"`
	Charset             string `yaml:"charset"`
	TimeOut             string `yaml:"timeout"`
	MaxIdleConns        int    `yaml:"maxIdleConns"`
	MaxOpenConns        int    `yaml:"maxOpenConns"`
	LogMode             string `yaml:"logMode"`
	EnableFileLogWriter bool   `yaml:"enableFileLogWriter"`
}

type Redis struct {
	RedisHost     string `yaml:"redisHost"`
	RedisPort     string `yaml:"redisPort"`
	RedisUsername string `yaml:"redisUsername"`
	RedisPassword string `yaml:"redisPwd"`
	RedisDbName   int    `yaml:"redisDbName"`
	RedisNetwork  string `yaml:"redisNetwork"`
}

type Cache struct {
	CacheType    string `yaml:"cacheType"`
	CacheExpires int64  `yaml:"cacheExpires"`
	CacheWarmUp  bool   `yaml:"cacheWarmUp"`
	CacheServer  string `yaml:"cacheServer"`
}

type Es struct {
	Url                 string `yaml:"url"`
	User                string `yaml:"user"`
	Password            string `yaml:"password"`
	Gzip                bool   `yaml:"gzip"`
	HealthcheckInterval int    `yaml:"healthcheckInterval"`
	MaxRetries          int    `yaml:"maxRetries"`
	Sniff               bool   `yaml:"sniff"`
}

type ZapLog struct {
	//Level      string `yaml:"level"`
	SaveBasePath string `yaml:"savePath"`
	MaxSize      int    `yaml:"maxSize"`
	MaxAge       int    `yaml:"maxAge"`
	Compress     bool   `yaml:"compress"`
	MaxBackups   int    `yaml:"maxBackups"`
}

type LogrusLog struct {
	//Level       string `yaml:"level"`
	ErrLogPath  string `yaml:"errLogPath"`
	WarnLogPath string `yaml:"warnLogPath"`
}

type JWTAuth struct {
	AccessSecret string `yaml:"accessSecret"`
	AccessExpire int64  `yaml:"accessExpire"`
	Issuer       string `yaml:"issuer"`
}

func InitConfig() {
	//workDir, _ := os.Getwd()
	viper.SetConfigName("config")
	viper.SetConfigType("yml")
	viper.AddConfigPath("./app/core")
	err := viper.ReadInConfig()
	if err != nil {
		log.Fatal("配置文件读取失败")
	}
	err = viper.Unmarshal(&Conf)
	if err != nil {
		log.Fatal("配置序列化失败")
	}

}
